# @Time    : 2018/8/14 14:02
# -*- coding: utf-8 -*-
# @Author  : 聂家利
# @Email   : 1073438012@qq.com
# @File    : book.py
# @Software: PyCharm

from flask import jsonify, render_template, flash
from flask import request
from flask_login import current_user

from app.forms.books import SearchForm
from app.models.gift import Gift
from app.models.wish import Wish
from app.spider.yushu_book import YuShuBook
from app.view_models.book import BookViewModel, BookCollection
from app.view_models.trade import TradeInfo
from .Blueprint import web


@web.route('/test')
def test():
    r = {
        'name': 'njl',
        'age': 18
    }
    flash('hello,njl')
    flash('hello,njlxx', category='error')
    return render_template('test3.html', data=r)


@web.route('/test1')
def test1():
    from flask import request
    from app.libs.none_local import n
    print(n.v)
    n.v = 2
    print('..................')
    print(getattr(request, 'v', None))
    setattr(request, 'v', 2)
    print('............')
    return ''
#
# @web.route('/api/', methods=['GET', 'POST'])
# def index():
#     # todo 验证
#     form = SearchForm(request.args)
#     if form.validate():
#         print(form.q.data, form.page.data)
#         return jsonify(request.args)
#     else:
#         return jsonify(form.errors)


@web.route('/book/search', methods=['GET', 'POST'])
def search():
    # todo 验证
    form = SearchForm(request.args)
    books = BookCollection()

    if form.validate():
        q = form.q.data
        page = form.page.data
        isbn_or_key = 'isbnx'
        yushu_book = YuShuBook()

        if isbn_or_key == 'isbn':
            yushu_book.search_by_isbn(q)
        else:
            yushu_book.search_by_keyword(q, page)

        books.fill(yushu_book, q)
        # return json.dumps(books, default=lambda o: o.__dict__)
    else:
        # jsonify(form.errors)
        flash('搜索的关键字不符合要求，请重新输入关键字')

    return render_template('search_result.html', books=books, form=form)


@web.route('/book/<isbn>/detail')
def book_detail(isbn):
    has_in_gifts = False
    has_in_wishes = False

    # 取书籍详情数据
    yushu_book = YuShuBook()
    yushu_book.search_by_isbn(isbn)
    book = BookViewModel(yushu_book.first)

    if current_user.is_authenticated:
        if Gift.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_gifts = True
        if Wish.query.filter_by(uid=current_user.id, isbn=isbn,
                                launched=False).first():
            has_in_wishes = True

    trade_gifts = Gift.query.filter_by(isbn=isbn, launched=False).all()
    trade_wishes = Wish.query.filter_by(isbn=isbn, launched=False).all()

    trade_wishes_model = TradeInfo(trade_wishes)
    trade_gifts_model = TradeInfo(trade_gifts)
    return render_template('book_detail.html', book=book, wishes=trade_wishes_model,
                           gifts=trade_gifts_model, has_in_gifts=has_in_gifts,
                           has_in_wishes=has_in_wishes)


# @web.route('/search/')
# def search():
#     return "XX"

#
# @web.route('/my_gifts/')
# def my_gifts():
#     return "XX"


# @web.route('/my_wish/')
# def my_wish():
#     return "XX"


@web.route('/pending/')
def pending():
    return "XX"

